<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-10-22 14:59:58
 * @LastEditors: charles
 * @LastEditTime: 2021-10-25 22:11:02
-->
<template>
  <b-fullpage title="订单确认">
    <div class="order_confirm_container">
      <div class="address">
        <div class="pca">
          <span class="default">默认</span>
          {{address.province+''+address.city+''+address.area}}
        </div>
        <div class="address">{{address.address}}</div>
        <div class="name_tel">{{address.realname+''+address.telephone}}</div>
      </div>

      <ul class="order_lines">
        <li class="order_line header">
          <div>名称</div>
          <div>数量</div>
          <div>价格</div>
        </li>
        <li class="order_line" v-for="l in orderVM.orderLines" :key="l.id">
          <div class="name">
            <div class="img">
              <img :src="l.photo" style="width:50px" alt="">
            </div>
            {{l.name}}
          </div>
          <div>{{l.num}}</div>
          <div>￥ {{l.price}}</div>
        </li>
      </ul>
      <div class="consume">
        总计：￥{{orderVM.total}}
      </div>
      <div class="buyerMessage">
        <van-field
          v-model="buyerMessage"
          rows="3"
          autosize
          label="买家留言"
          type="textarea"
          placeholder="请输入留言"
        />
      </div>
      <div class="btns">
        <van-button @click="submitOrderHandler" size="small" type="primary">提交订单</van-button>
      </div>
    </div>
  </b-fullpage>
</template>
<script>
import {useRoute,useRouter} from 'vue-router'
import { onMounted,ref ,computed} from 'vue';
import { useStore } from 'vuex'
import { get, postJSON } from '../../../utils/request'
export default {
  
  setup() {
    let route = useRoute();
    let store = useStore();
    let router = useRouter();
    let orderLines = ref([]);
    let address = ref({});
    let buyerMessage = ref('');
    // 获取用户的默认地址
    let loadDefaultAddress = async function(){
      let url = "/app/address/queryDefaultAddress"
      let resp = await get(url,{userId:user_info.value.id})
      address.value = resp.data;
    }
    // 提交订单
    let submitOrderHandler =async function(){
      let url = "/app/order/submitOrder"
      let resp = await postJSON(url,orderVM.value)
      router.push("/home/orders")
    }

    let user_info = computed(()=> store.state.user.user_info)
    let orderVM = computed(()=>{
      let vm = {};
      vm.customerId = user_info.value.id;
      vm.addressId = address.value.id;
      let total = 0;
      orderLines.value.forEach(item=>{
        total += item.price * item.num;
      })
      vm.total = total;
      vm.buyerMessage = buyerMessage.value;
      vm.orderLines = orderLines.value.map(item=>{
        return {
          name:item.name,
          num:item.num,
          price:item.price,
          photo:item.photo,
          productId:item.id
        }
      })
      return vm;
    });

    onMounted(async ()=>{
      orderLines.value = JSON.parse(route.params.orderLines);
      await loadDefaultAddress();
    })

    return {
      user_info,
      address,
      orderVM,
      submitOrderHandler,
      buyerMessage
    }
  },
}
</script>
<style lang="scss" scoped>
.order_confirm_container {
  .buyerMessage {
    margin-bottom:1em;
  }
  
  .consume {
    background-color: #fff;
    padding:.5em 1em;
    margin-bottom :1em;
    text-align: right;
  }
  .address {
    background-color: #fff;
    padding:.5em 1em;
    margin-bottom :1em;
    .pca {
      font-size: 12px;
      .default{
        padding: 2px 4px;
        font-size: 10px;
        background-color: red;
        color:#fff;
      }
    }
    .address {
      font-weight: bold;
      padding: .6em 0;
    }
    .name_tel {
      font-size: 12px;
    }
  }
  ul.order_lines {
    padding: .5em 1em;
    background-color: #fff;
    border-bottom: 1px solid #efefef;
    li.header {
      font-weight: bold;
    }
    li.order_line {
      display: flex;
      & > div:nth-child(1) {
        margin-bottom: .5em;
        .img {
          width: 40px;
          height: 30px;
          overflow: hidden;
          border-radius: 3px;
          margin-right:5px;
          float: left;
          img {
            width: 100%;
          }
        }
        flex:1;
      }
      & > div:nth-child(2),
      & > div:nth-child(3)
      {
        text-align: center;
        flex-basis: 56px;
      }
    }
  }
  .btns {
    text-align: right;
    padding: 0 1em;
  }
}
</style>